
app.controller('addRoleCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope, $compile, $timeout) {
	$scope.iNowData = angular.copy($scope.ngDialogData, {});
	var token = $window.localStorage.getItem("token");
    console.log($scope.iNowData)
	$scope.dataTableMember = {
        "sAjaxSource": "/tenancy-sys/admin/user/list", //http://121.40.178.164:7171/settlement-center-sys/admin/role/list.do
        // "dom": "tipr",
        "bLengthChange": true,
        "sScrollY": 400,  //给定高度，内容超出即出滚动条
         "bScrollCollapse": false,  //不管内容多少高度固定
         "aLengthMenu": [20,50,80,100,120],
         "iDisplayLength": 20,
         "sDom": '<"top">rt<"bottom"lip><"clear">', //位置 l-每页显示数量,f-过滤输入,t-表单Table,i-信息,p-翻页
        "aoColumns": [
            {
                "mDataProp": "userName",
                "sTitle": "姓名",
                "sDefaultContent": "",
                "sClass": "center failure"
            }, {
                "mDataProp": "loginName",
                "sTitle": "账户",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "operate",
                "sTitle": "操作",
                "sDefaultContent": "",
                "sClass": "center handle-td"
            }
        ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex) {
            var btnStr = "";
            btnStr += "<a href='javascript:;' class='btn btn-yellow' style='margin-right:10px;' ng-click='add($event, \"" + iDisplayIndex + "\")'>添加</a>";
            
            $(nRow).find(".handle-td").append($compile(btnStr)($scope));
            if (iDisplayIndex >= 9) {
                
            }
        },
        "fnDrawCallback": function(oSettings) {
            /*$scope.houseLoanStatusValue = $scope.queryParams.houseLoanStatus;
            if ("number" == typeof $scope.houseLoanStatusValue) {
                $scope.checkTypeArray.map(function(ele, index) {
                    if ($scope.houseLoanStatusValue == ele.value) {
                        $scope.houseLoanStatus.selected = ele;
                    }
                })
            }*/
        },
        "fnServerParams": function(aoData) {
            /*if($scope.houseLoanStatus && $scope.houseLoanStatus.selected){
                $scope.queryParams.houseLoanStatus = $scope.houseLoanStatus.selected.value
            }

            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;*/
        },
        "fnServerData": function(sSource,aData,fnCallback,dataTableObject) {
           var obj = dataTableObject.oAjaxData,_baseParams = {
             // 'landlordId':user.get('landlordId'),
             'page':obj.iDisplayStart / obj.iDisplayLength + 1,
             'rows':obj.iDisplayLength,
             'search':'false',
             'nd': '1494221804416',
             // 'rows': 50,
             // 'page': 1,
             'order': "asc",
             'status': 'AVAILABLE'
           };

           var __data = angular.extend({},_baseParams,obj.queryParams);
            rest.decoJava({
	           	type: 'post',
	            url: sSource,
	            data: __data,
	           	beforeSend: function(request){
					request.setRequestHeader("token", token);
				},
	            success: function(data){    
		            console.log(data);
		            $scope.myDatas = data;
		                var data1 = {
		                     'aaData': data.result,
		                     'sEcho': obj.sEcho,
		                     'iTotalDisplayRecords': data.pager.recordCount,
		                     'iTotalRecords': data.records,
		                };
		            fnCallback(data1);
	            }
            })
        }
    }
	
	//添加成员
    $scope.add = function (e, iDisplayIndex) {
    	//var addRoleName = $scope.myDatas.result[iDisplayIndex];
    	/*
    	$scope.iNowData.id  被添加的角色名称id
    	$scope.myDatas.rows[iDisplayIndex].id)  正要添加的角色id
    	*/
    	rest.decoJava({
	   		type: 'post',
            url: '/tenancy-sys/admin/role/add/user',
            data: {roleId: $scope.iNowData.id, userIds: $scope.myDatas.result[iDisplayIndex].id},
            beforeSend: function(request){
				request.setRequestHeader("token", token);
			},
            success: function(data){
            	if("0000" == data.code){
            		//$scope.roles.push(addRoleName); //提交的同时前端伪添加
	                layer.msg(data.message, {
				    	offset:'t',
				      	icon: 6
				    });
				    $scope.closeThisDialog();
            	} else{
            		layer.msg(data.message, {
				    	offset:'t',
				      	icon: 5,
				      	btn: ['确定']
				    });
            	}
                
            }
        })
		
    }
	
})